package com.changgou.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.item.service.PageService;
import com.changgou.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*****
 * @Author: 黑马训练营
 * @Description: com.changgou.item.service.impl
 ****/
@Service
public class PageServiceImpl implements PageService {

    //静态页存储路径
    @Value("${pagepath}")
    private String pagepath;

    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private SkuFeign skuFeign;

    /***
     * 加载所有静态页需要的数据
     */
    public Map<String,Object> buildDataModel(String spuid){
        Map<String,Object> dataMap = new HashMap<String,Object>();

        //Spu数据
        Result<Spu> resultSpu = spuFeign.findById(spuid);
        Spu spu = resultSpu.getData();

        //三个分类数据
        dataMap.put("category1",categoryFeign.findById(spu.getCategory1Id()).getData());
        dataMap.put("category2",categoryFeign.findById(spu.getCategory2Id()).getData());
        dataMap.put("category3",categoryFeign.findById(spu.getCategory3Id()).getData());

        //List<Sku>集合
        Sku csku = new Sku();
        csku.setSpuId(spuid);
        Result<List<Sku>> skuResult = skuFeign.findList(csku);
        List<Sku> skuList = skuResult.getData();
        dataMap.put("skuList",skuList);

        //商品图片
        dataMap.put("imageList",spu.getImages().split(","));
        //将spu中的规格集合转成Map
        dataMap.put("specList", JSON.parseObject(spu.getSpecItems(),Map.class));
        return dataMap;
    }

    /***
     * 使用Thymeleaf生成商品详情页
     * 根据SPUID查询商品详情页所需的数据
     * @param spuId
     */
    @Override
    public void createPageHtml(String spuId) {
        try {
            //创建上下文对象 Context,用于存储Thymeleaf所需的属性以及数据信息
            Context context = new Context();

            //准备页面的数据 Map<String,Object>->数据模型
            Map<String,Object> dataModel = buildDataModel(spuId);
            context.setVariables(dataModel);

            //生成静态页(生成文件),指定生成文件的名字以及文件生成的路径
            File file = new File(pagepath,spuId+".html");

            //创建文件输出对象，将文件写入到磁盘中
            PrintWriter writer = new PrintWriter(file,"UTF-8");
            //Thymeleaf模板引擎生成文件
            templateEngine.process("item",context,writer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
